<?php

namespace app\api\controller;

use app\common\controller\ApiController;
use think\App;
use \app\admin\traits\Curd;
use think\facade\Cache;
use think\facade\Db;
use think\Http;

class Adv extends ApiController
{

    protected $noNeedLogin = ['real_authentication'];
    protected $mustLogin = ['real_authentication'];

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
 
   /**
     * @desc 该方法向用户发送平台通知和领取优惠券通知
     * @param $openid 接收消息用户的openid
     * @param $arr 消息内容，根据模板需要进行配置
     */
    public function real_authentication($openid,$arr,$type){

        //类型 type 1景区通知   2优惠券到账通知   3预约成功通知
        if($type == 1){
            // 站内信标题:{{thing6.DATA}}
            // 站内信内容:{{phrase1.DATA}}
            // 时间:{{date2.DATA}}

            // 站内信标题:道路拥堵提醒
            // 站内信内容:求助被完成
            // 时间:2019-10-24 
            //小程序后台配置的模板id
            $template_id = "c9qVAWcE1Cgmd_y7GtnX3N54zCI41gfNtZn1fBZWmgg";
            $template = "{
                \"touser\": \"{$openid}\",
                \"template_id\": \"{$template_id}\",
                \"page\": \"index\",
                \"miniprogram_state\":\"developer\",
                \"lang\":\"zh_CN\",
                \"data\": {
                    \"thing6\": {
                        \"value\": \"{$arr['title']}\"
                    },
                    \"phrase1\": {
                        \"value\": \"{$arr['content']}\"
                    },
                    \"date2\": {
                        \"value\": \"{$arr['time']}\"
                    }
                }
              }";
        }elseif($type == 2){
            // 优惠券名称:{{thing5.DATA}}
            // 优惠券金额:{{amount2.DATA}}
            // 过期时间:{{time7.DATA}}
            // 温馨提示:{{thing4.DATA}}

            // 优惠券名称 :新人优惠券
            // 优惠券金额:100元
            // 过期时间:2020年12月31日
            // 温馨提示:可前往个人中心-优惠券界面查看
            $template_id = "FfK1ISvCx5JhzOHCIGu2fVr4x7zthCbikg9HJAvcNNE";
            $template = "{
                \"touser\": \"{$openid}\",
                \"template_id\": \"{$template_id}\",
                \"page\": \"index\",
                \"miniprogram_state\":\"developer\",
                \"lang\":\"zh_CN\",
                \"data\": {
                    \"thing5\": {
                        \"value\": \"{$arr['title']}\"
                    },
                    \"amount2\": {
                        \"value\": \"{$arr['content']}\"
                    },
                    \"time7\": {
                        \"value\": \"{$arr['time']}\"
                    },
                    \"thing4\": {
                        \"value\": \"{$arr['time']}\"
                    }
                }
              }";

        }elseif($type == 3){
            // 订单号:{{character_string6.DATA}}
            // 预约项目:{{thing7.DATA}}
            // 预约时段:{{character_string8.DATA}}
            // 金额:{{amount9.DATA}}
            // 地址:{{thing2.DATA}}

            // 订单号:12222222122222
            // 预约项目:房屋清洁
            // 预约时段:18:00-22:00
            // 金额:0.1
            // 地址:广东省广州市

            $template_id = "Jdr1nGIVy-BpYyR8slvtvZebxio5AetQKjpYqht9Z-0";
            $template = "{
                \"touser\": \"{$openid}\",
                \"template_id\": \"{$template_id}\",
                \"page\": \"index\",
                \"miniprogram_state\":\"developer\",
                \"lang\":\"zh_CN\",
                \"data\": {
                    \"character_string6\": {
                        \"value\": \"{$arr['order_no']}\"
                    },
                    \"thing7\": {
                        \"value\": \"{$arr['title']}\"
                    },
                    \"character_string8\": {
                        \"value\": \"{$arr['time_start']}\"
                    },
                    \"amount9\": {
                        \"value\": \"{$arr['payable_money']}\"
                    },
                    \"thing2\": {
                        \"value\": \"{$arr['address']}\"
                    }
                }
              }";
        }

 
          //获取access_token
          $access_token = $this->get_access_token();
          //使用curl进行请求
          $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$access_token}";
          //$this->curl_post($url,$template)
          $res = $this->curl_post($url,$template);
          return $res;
          //成功："{"errcode":0,"errmsg":"ok","msgid":1822356413101195268}"
          //失败："{"errcode":43101,"errmsg":"user refuse to accept the msg rid: 6073f992-2337e5cf-0dee24c3"}"   //用户没有订阅消息时的错误提示
      }
  
    //curl请求的方法
    private function curl_post($url,$data=array()){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
          // POST数据
          curl_setopt($ch, CURLOPT_POST, 1);
          // 把post的变量加上
          curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
          $output = curl_exec($ch);
          curl_close($ch);
          return $output;
      }
  
  
      /**
       * @desc 获取access_token
       */
      private function get_access_token(){
      
      $appid = 'wx6e95e7efa60d658d';
      $secretkey = '6211a9823ba64c8f839b1d3b1419d3bf';
      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secretkey ;
      $res = json_decode(file_get_contents($url),true);//获取微信返回的access_token
      $access_token = $res['access_token'];
      //获取到access_token应该保存起来，一般7000秒内不会过期
      Cache::set('accessToken',$access_token, 5400);
      return Cache::get('accessToken');
  }

 
   

}
